var express = require('express');
var router = express.Router();
var package = require("../package.json");
//var business = require("../app_modules/business.js");
var dbHelper = require("../app_modules/mongoHelper.js");
var tool = require("../app_modules/tools.js");
var jwtTool = require("../app_modules/JwtTools.js");

var UserCollection="User";

router.post('/functionRouter', function (req, res, next) {
   if(!jwtTool.getTokenObj(req).status)
   return res.send({status:"0",msg:"please login.",metadata:{}}); 
   var functionName=tool.getObj(req.body.Function,"");
   var metadata=tool.getObj(req.body.MetaData,"");
   console.log(metadata);
   if(functionName==""){return res.send({status:"0",msg:"function is required.",metadata:""});} 
   else if(functionName=="getuser"){getuser(metadata,function(result){return res.send(result);});}
   else if(functionName=="adduser"){adduser(metadata,function(result){return res.send(result);});}
   else if(functionName=="updateuser"){updateuser(metadata,function(result){return res.send(result);});}
   else{return res.send({status:"0",msg:"function is not exist.",metadata:""});} 
});

//#region user management
function getuser(metadata,callback){
    var parameter=JSON.parse(metadata);
    console.log(parameter);
    var filter={};
    var includeItem={_id:0,password:0};
    var sort={"code":1};
    var pageIndex=parameter.pageIndex;
    var pageSize=parameter.pageSize;       
     dbHelper.find(UserCollection,filter,includeItem,sort,pageIndex,pageSize,function(data){        
        return callback(data); ;
     });
}
function adduser(metadata,callback){
     var parameter=JSON.parse(metadata);  
     if(parameter.code=="")return callback({status:"0",msg:"add faild,because code field is empty."});  
     var model=parameter;    
     dbHelper.create(UserCollection,model,function(data){
        return callback(data); ;
     });
}
function updateuser(metadata,callback){
    var parameter=JSON.parse(metadata); 
    if(parameter.code=="")return callback({status:"0",msg:"add faild,because code field is empty."});   
    var query={code:parameter.code};
     var model=parameter;    
     dbHelper.update(UserCollection,query,model,function(data){
        return callback(data); ;
     });
}
//#endregion

//#region  login and login out、gettoken info
router.post('/login', function (req, res, next) {
    var accout=tool.getObj(req.body.accout,"");
    var password=tool.getObj(req.body.password,"");

    var filter={'code' : accout,'password' : password}; 
    var includeItem={_id:0,password:0};
    dbHelper.findOne(UserCollection,includeItem,filter,function(data){
        if(data.status==true){
            var result=data.metadata;
            var url=package.apiUrl+package.filePath.replace("./public/","");
            result.photourl=url+result.photo;
            var token=jwtTool.jwtCreateToken(result);    
            res.cookie('token',token,{maxAge:1000*60*60*24*7}); //有效期以毫秒为单位
            res.send({status:"1",token:token,msg:"login successfully",metadata:result});
        }  
        else{
            res.send({status:"0",token:"",msg:"login faild"})
        } 
    });   
 });


 router.post('/loginOut', function (req, res, next) {
    res.cookie('token',"",{maxAge:1000*60*60*24*7}); //有效期以毫秒为单位
    res.send({status:"1",token:"",msg:"login out successfully."})
 });

 router.post('/getUserByToken', function (req, res, next) {
    res.send(jwtTool.getTokenObj(req))
 });
//#endregion



module.exports = router;    